<?php

//数据参照控制器---fhc
class DatareferApp extends BackendApp
{
	var $_waybill_mod;
	var $_dictionary_mod;
	var $_user_mod;
	var $canedit;
	var $_haul_mod;
	function __construct()
	{
		$this->DatareferApp();
	}

	function DatareferApp()
	{
		parent::BackendApp();

		$this->_waybill_mod =& m('waybill');
		$this->_dictionary_mod =& m('dictionary');
		$this->_user_mod =& m('member');
		$this->_haul_mod =& m('haul');
		$this->canedit=false;
	}
	//获取货主
	function gethz(){
		$db=&db();
		$result = "{";
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];
			if($k){
				$sql="select * from zys_operator where (o_name like '%".$k."%' or o_name like '%".o_code."%') and o_consignor=1 limit 5";
				$list=$db->getAll($sql);
				if($list){
					foreach ($list as $key=>$value) {
						$result .='"'.$value['o_tid'].';'.$value['o_linkman'].';'.$value['o_linktel'].'":"'.$value['o_name'].'",';
					}
				}
			}
			$result=trim($result,',');
			$result .= "}";
			echo $result;
		}catch(Exception $e){
			echo $result;
		}
	}
	function gethzinfo(){
		$db=&db();
		$result = array();
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];

			if(empty($k)){
				throw new Exception("");
			}

			$sql="select o_tid as id,o_name as text, o_linktel ,o_linkman from zys_operator where o_name='".$k."' and o_consignor=1";
			$result=$db->getRow($sql);

			if(empty($result)){
				throw new Exception("");
			}

			echo $this->json_result($result);
		}catch(Exception $e){
			echo $this->json_error($e->getMessage());
		}
	}
	//获取承运商
	function getcys(){
		$db=&db();
		$result = "{";
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];
			if($k){
				$sql="select * from zys_operator where (o_name like '%".$k."%' or o_name like '%".o_code."%') and o_carrier=1 limit 5";
				$list=$db->getAll($sql);

				if($list){
					foreach ($list as $key=>$value) {
						$result .='"'.$value['o_tid'].'":"'.$value['o_name'].'",';
					}
				}
			}
			$result=trim($result,',');
			$result .= "}";
			echo $result;
		}catch(Exception $e){
			echo $result;
		}
	}
	function getcysinfo(){
		$db=&db();
		$result = array();
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];

			if(empty($k)){
				throw new Exception("");
			}

			$sql="select o_tid as id,o_name as text from zys_operator where o_name='".$k."' and o_carrier=1";
			$result=$db->getRow($sql);

			if(empty($result)){
				throw new Exception("");
			}

			echo $this->json_result($result);
		}catch(Exception $e){
			echo $this->json_error($e->getMessage());
		}
	}
	//获取地址
	function getaddr(){
		$userid = $this->visitor->get('user_id');
		$unit = $this->visitor->get('unit');
		$db=&db();
		$result = "{";
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];
			if($k){
				$sql="select * from zys_addr where address like '%".$k."%' and user_id=".$userid." and unit=".$unit." limit 5";
				$list=$db->getAll($sql);

				if($list){
					foreach ($list as $key=>$value) {
						$result .='"'.$value['lng'].';'.$value['lat'].'":"'.$value['address'].'",';
					}
				}
			}
			$result=trim($result,',');
			$result .= "}";
			echo $result;
		}catch(Exception $e){
			echo $result;
		}
	}
	function getaddrinfo(){
		$userid = $this->visitor->get('user_id');
		$unit = $this->visitor->get('unit');
		$db=&db();
		$result = array();
		try {
			$k=empty($_REQUEST['q'])?'':$_REQUEST['q'];

			if(empty($k)){
				throw new Exception("");
			}

			$sql="select addr_id as id,address as text,lng,lat from zys_addr where address='".$k."' and user_id=".$userid." and unit=".$unit;
			$result=$db->getRow($sql);

			if(empty($result)){
				throw new Exception("");
			}

			echo $this->json_result($result);
		}catch(Exception $e){
			echo $this->json_error($e->getMessage());
		}

	}
	//车牌号列表
	function choose()
	{
		$db=&db();

		$oper_id=$_GET['oper'];

		$this->assign('oper', $oper_id);

		if(empty($oper_id)){
			$oper_id=$_POST['oper'];
			$this->assign('oper', $oper_id);
		}

		$conditions = $this->_get_query_conditions(array(
			array(
				'field' => $_GET['field_name'],
				'name'  => 'field_value',
				'equal' => 'like',
			),
		));
		//更新排序
		if (isset($_GET['sort']) && !empty($_GET['order']))
		{
			$sort  = strtolower(trim($_GET['sort']));
			$order = strtolower(trim($_GET['order']));
			if (!in_array($order,array('asc','desc')))
			{
				$sort  = 'last_login';
				$order = 'desc';
			}
		}
		else
		{
			if (isset($_GET['sort']) && empty($_GET['order']))
			{
				$sort  = strtolower(trim($_GET['sort']));
				$order = "";
			}
			else
			{
				$sort  = 'last_login';
				$order = 'desc';
			}
		}
		$page = $this->_get_page();

		if(!empty($oper_id)){
			$sql="SELECT * FROM zys_authoperator a LEFT JOIN zys_member m ON a.zao_usertid=m.user_id
			WHERE zao_otid=$oper_id AND zao_authed=1 AND ifnull(m.type,0)<>0 ". $conditions." order by $sort $order limit $page[limit]";
			$users =$db->getAll($sql);

			$sql="SELECT COUNT(1) FROM zys_authoperator a LEFT JOIN zys_member m ON a.zao_usertid=m.user_id
			WHERE zao_otid=$oper_id AND zao_authed=1 AND ifnull(m.type,0)<>0 ". $conditions;
			$page['item_count']  =$db->getOne($sql);
		}else{
			$sql="SELECT * FROM zys_member WHERE IFNULL(type,0)<>0 ". $conditions." order by $sort $order limit $page[limit]";
			$users =$db->getAll($sql);

			$sql="SELECT count(1) FROM zys_member WHERE IFNULL(type,0)<>0 ". $conditions;
			$page['item_count']  =$db->getOne($sql);
		}

		foreach ($users as $key => $val)
		{
			if ($val['priv_store_id'] == 0 && $val['privs'] != '')
			{
				$users[$key]['if_admin'] = true;
			}
		}
		$this->assign('users', $users);
		$this->assign('wid', $_GET['wid']);
		$page['item_count'] = $this->_user_mod->getCount();
		$this->_format_page($page);
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->assign('page_info', $page);
		$this->assign('query_fields', array(
			'vhcCode' => '车牌号',
			'real_name'     => '司机',
			'phone_tel' => '手机号',
			//            'phone_tel' => LANG::get('phone_tel'),
			//            'phone_mob' => LANG::get('phone_mob'),
		));

		$this->display('datarefer.choose.html');
	}
	function choosedone(){
		$uid=empty($_GET['id'])?0:intval($_GET['id']);
		$wid=empty($_GET['wid'])?0:intval($_GET['wid']);
		$oper=empty($_GET['oper'])?0:intval($_GET['oper']);
		$db=&db();

		$sql="select * from zys_member where user_id=".$uid;
		$user=$db->getRow($sql);
		$sql="select * from zys_waybill_main where wm_tid=".$wid;
		$w=$db->getRow($sql);

		$sql="SELECT o_name from zys_operator WHERE o_tid=".$oper;
		$oname=$db->getOne($sql);
		if(!$oname){
			$sql="SELECT o.o_name FROM zys_authoperator a LEFT JOIN zys_operator o ON o.o_tid=a.zao_otid
			WHERE zao_usertid=$uid AND zao_authed=1 ORDER BY zao_auth_date DESC LIMIT 1";
			$oname=$db->getOne($sql);
		}

		if($user){
			$sql="update zys_waybill_main set wm_user='".$uid."',wm_uuid='".$user['uuid']."',wm_dispatch_date='".gmtime()."',wm_dispatch_person='".$this->visitor->get('user_name')."',
			wm_dispatch_person_tel='".$this->visitor->get('phone_mob')."',wm_vhc_code='".$user['vhcCode']."',wm_driver='".$user['real_name']."'
			,wm_driver_tel='".$user['phone_tel']."',wm_carrier='".$oname."',wm_carrier_id=$oper where wm_tid=".$wid;
			$db->query($sql);
		}
		echo("<script>window.parent.location.href=window.parent.location.href;</script>");
	}
	//承运商列表
	function chooseop(){
		$db=&db();
		$page = $this->_get_page();

		$wmid=empty($_GET['wid'])?'':$_GET['wid'];
		if(!empty($wmid)){
			$this->assign('wm_tid', $wmid);
		}

		$o_name=empty($_GET['o_name'])?'':$_GET['o_name'];

		$sqlw=" o_carrier=1 ";
		if($o_name){
			$sqlw.=" and o_name like '%".$o_name."%'";
		}

		$sql="SELECT * FROM zys_operator WHERE $sqlw";
		$page['item_count'] = $db->getOne($sql);

		$this->_format_page($page);
		$this->assign('page_info', $page);

		$sql="SELECT * FROM zys_operator WHERE $sqlw limit $page[limit]";
		$op=$db->getAll($sql);
		$this->assign('op', $op);

		$this->display('datarefer.chooseop.html');
	}
	function chooseopdone(){
		$id=empty($_GET['id'])?0:$_GET['id'];
		$name=empty($_GET['name'])?'':$_GET['name'];
		echo("<script>window.parent.changeop($id,'".$name."');</script>");
	}
	//货主列表
	function choosehz(){
		$db=&db();
		$page = $this->_get_page();

		$o_name=empty($_GET['o_name'])?'':$_GET['o_name'];

		$hzid=empty($_GET['hzid'])?0:$_GET['hzid'];
		$this->assign('hzid', $hzid);

		$sqlw=" o_consignor=1 ";
		if($o_name){
			$sqlw.=" and o_name like '%".$o_name."%'";
		}
		$sql="select * from zys_operator WHERE $sqlw";
		$page['item_count'] = $db->getOne($sql);

		$this->_format_page($page);
		$this->assign('page_info', $page);

		$sql="SELECT * FROM zys_operator WHERE $sqlw limit $page[limit]";
		$hz=$db->getAll($sql);
		$this->assign('hz', $hz);

		$this->display('datarefer.choosehz.html');
	}
	function choosehzdone(){
		$id=empty($_GET['id'])?0:$_GET['id'];
		$name=empty($_GET['name'])?'':$_GET['name'];
		$hzid=empty($_GET['hzid'])?0:$_GET['hzid'];
		$linkman=empty($_GET['linkman'])?'':$_GET['linkman'];
		$linktel=empty($_GET['linktel'])?'':$_GET['linktel'];
		echo("<script>window.parent.changehz($id,'".$name."',$hzid,'".$linkman."','".$linktel."');</script>");
	}
	//监控方案详情
	function monitorinfo(){
		$mid = empty($_GET['mid']) ? 0 : intval($_GET['mid']);
		$db=&db();
		$sql = "select count(*) as num from zys_monitor where m_zmmtid =".$mid;
		$page = $this->_get_page();
		$page['item_count'] = $db->getOne($sql);
		$this->_format_page($page);
		$sql="select * from zys_monitor where m_zmmtid =".$mid." limit $page[limit]";
		$monitors=$db->getAll($sql);
		$this->assign('page_info', $page);
		$this->assign('monitors', $monitors);
		$this->display('datarefer.monitorinfo.html');
	}
	//取地址经纬度
	function mapselect(){
		$lng=empty($_GET['lng'])?"":$_GET['lng'];
		$lat=empty($_GET['lat'])?"":$_GET['lat'];
		$this->assign('ret', $_GET['ret']);
		$this->assign('address', $_GET['address']);
		$this->assign('lng', $_GET['lng']);
		$this->assign('lat', $_GET['lat']);
		$this->display('datarefer.mapselect.html');
	}
}